﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://www.codeplex.com/EmlenMud
using System;
using System.Collections.Generic;
using System.Threading;

namespace BrainTechLLC
{
    public static class Populate
    {
        public static string AddBackslashIfNecessary(this string s)
        {
            if (!s.EndsWith("\\"))
                return s + "\\";

            return s;
        }

        public static T CreateIfNull<T>(ref T item) where T : class, new()
        {
            if (item == null) { Interlocked.CompareExchange<T>(ref item, new T(), null); }
            return item;
        }

        public static T CreateIfNull<T>(ref T item, T value) where T : class
        {
            if (item == null) { Interlocked.CompareExchange<T>(ref item, value, null); }
            return item;
        }

        public static TInterface CreateIfNull<TInterface, TClass>(ref TInterface item)
            where TInterface : class
            where TClass : class, TInterface, new()
        {
            if (item == null) { Interlocked.CompareExchange<TInterface>(ref item, new TClass(), null); }
            return item;
        }

        public static T Find<T>(this IEnumerable<T> list, Func<T, bool> match) where T : class
        {
            foreach (T item in list)
            {
                if (match(item))
                    return item;
            }
            return null;
        }

        public static List<T> FindAll<T>(this IEnumerable<T> list, Func<T, bool> match) where T : class
        {
            List<T> results = new List<T>();
            foreach (T item in list)
            {
                if (match(item))
                    results.Add(item);
            }
            return results;
        }

        public static T OnDemand<T>(ref T item) where T : class, new()
        {
            if (item == null)
                Interlocked.CompareExchange<T>(ref item, new T(), null);

            return item;
        }

        public static T OnDemand<T>(ref T item, Func<T> func) where T : class
        {
            if (item == null)
                Interlocked.CompareExchange<T>(ref item, func(), null);

            return item;
        }

        public static T1 OnDemand<T1, T2>(ref T1 item, Func<T2> func)
            where T1 : class
            where T2 : class
        {
            if (item == null)
                Interlocked.CompareExchange<T1>(ref item, func() as T1, null);

            return item;
        }

        public static T1 OnDemand<T1, T2>(ref T1 item)
            where T1 : class
            where T2 : class, new()
        {
            if (item == null)
                Interlocked.CompareExchange<T1>(ref item, new T2() as T1, null);

            return item;
        }

        public static T1 PopulateIf<T1>(this T1 value, ref T1 varToCheck, TPopulate<T1> del)
        {
            if (value.Equals(varToCheck))
                varToCheck = del();
            
            return varToCheck;
        }
    }
}